package com.et114.components.jcaptcha;

import com.octo.captcha.service.CaptchaService;
import com.octo.captcha.service.CaptchaServiceException;
import org.acegisecurity.captcha.CaptchaServiceProxy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 实现 CaptchaServiceProxy 用于acegi来校验，由spring注入jcaptchaService
 * 
 * @author guanhw
 */
public class JCaptchaServiceProxyImpl implements CaptchaServiceProxy {
	protected static final Log	logger	= LogFactory.getLog( JCaptchaServiceProxyImpl.class );
	private CaptchaService		jcaptchaService;

	public boolean validateReponseForId ( String id , Object response ) {
		logger.debug( "validating captcha response" );
		try {
			logger.info(  "校验ID:" + id  ) ;
			boolean isHuman = jcaptchaService.validateResponseForID( id , response ).booleanValue();
			if ( isHuman ) {
				logger.debug( "captcha passed" );
			} else {
				logger.warn( "captcha failed" );
			}
			return isHuman;

		} catch ( CaptchaServiceException cse ) {
			logger.error( "验证码校验异常：", cse );
			return false;
		}
	}

	public void setJcaptchaService ( CaptchaService jcaptchaService ) {
		this.jcaptchaService = jcaptchaService;
	}
}
